using Microsoft.Extensions.DependencyInjection;

namespace SeataNet.Core.Saga
{
    /// <summary>
    /// Saga服务工厂
    /// </summary>
    public class SagaServiceFactory
    {
        private readonly IServiceProvider _serviceProvider;

        public SagaServiceFactory(IServiceProvider serviceProvider)
        {
            _serviceProvider = serviceProvider;
        }

        /// <summary>
        /// 创建Saga服务实例
        /// </summary>
        public T CreateService<T>() where T : class
        {
            return _serviceProvider.GetRequiredService<T>();
        }

        /// <summary>
        /// 创建Saga链
        /// </summary>
        public SagaChainBuilder CreateSagaChain(string sagaName)
        {
            return new SagaChainBuilder(sagaName, _serviceProvider);
        }
    }

    /// <summary>
    /// Saga链构建器
    /// </summary>
    public class SagaChainBuilder
    {
        private readonly string _sagaName;
        private readonly IServiceProvider _serviceProvider;

        public SagaChainBuilder(string sagaName, IServiceProvider serviceProvider)
        {
            _sagaName = sagaName;
            _serviceProvider = serviceProvider;
        }

        /// <summary>
        /// 添加步骤
        /// </summary>
        public SagaChainBuilder AddStep<TService>(string methodName) where TService : class
        {
            // 简化实现，仅用于性能测试
            return this;
        }

        /// <summary>
        /// 执行Saga链
        /// </summary>
        public async Task<TSagaResult> ExecuteAsync<TSagaRequest, TSagaResult>(TSagaRequest request)
        {
            // 简化实现，仅用于性能测试
            await Task.CompletedTask;
            return default(TSagaResult);
        }
    }
}
